package com.yycx.module.bus.provider.handler.process.form;

import com.yycx.bpm.provider.handler.TaskCompleteHandler;
import com.yycx.bpm.provider.model.TaskRequest;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.provider.mapper.BaseRoleUserMapper;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.*;
import com.yycx.msg.provider.websocket.WebSocketServerEndpoint;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取任务表单数据
 *
 * @author zyf
 */
@Component
public class OrderTaskDataHandler implements TaskCompleteHandler {


    @Autowired
    private BusOrderService orderService;

    @Resource
    private WebSocketServerEndpoint socketServerEndpoint;
    @Resource
    private BaseRoleUserMapper roleUserMapper;


    @Override
    public Map<String, Object> initTaskData(ProcessInstance processInstance, Task task, Map<String, Object> variables) {
        Map<String, Object> ret = new HashMap<>(5);
        String businessKey = processInstance.getBusinessKey();

        return ret;
    }

    /**
     * 表单提交后业务处理
     *
     * @param taskRequest
     * @param processInstance
     * @param task
     * @param completeVariables
     */
    @Override
    public void completeAfter(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {
        String businessKey = processInstance.getBusinessKey();
        String name = task.getName();
        BusOrder order = orderService.getById(businessKey);
        Long companyId = order.getCompanyId();
        if (FlymeUtils.isNotEmpty(name)) {
            String orderNo = order.getOrderNo();
            if (name.equals("出纳认款")) {
                List<Long> userIds = roleUserMapper.selectUserIdByRoleCode("ROLE_CW", companyId);
                socketServerEndpoint.sendMessage(2L, "订单审核", "单号:" + orderNo+",出纳已认款", userIds, 2);
            }
        }


    }

}
